package mercurius.fitness.client;


import java.util.HashMap;
import java.util.Vector;

import mercurius.fitness.client.rb.exercise.ExerciseRB;
import mercurius.fitness.client.rb.exercise.forms.AddExerciseForm;
import mercurius.fitness.client.rb.measurement.MeasurementsRB;
import mercurius.fitness.client.rb.measurement.forms.AddMeasurementsForm;
import mercurius.fitness.client.rb.summary.forms.ViewSummaryForm;
import mercurius.fitness.client.rb.user.forms.EditUserForm;
import mercurius.fitness.client.ui.menu.MainMenu;
import mercurius.fitness.client.util.HTTPUtil;
import mercurius.fitness.client.util.Logger;
import net.pleso.framework.client.ui.controls.calendar.CalendarWidget;
import net.pleso.framework.client.ui.custom.CustomFormWindow;
import net.pleso.framework.client.ui.custom.CustomRBWindow;
import net.pleso.framework.client.ui.windows.TabbedSliderManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Fitness implements EntryPoint {

	TabbedSliderManager windowsManager = TabbedSliderManager.getInstance();
	
	CalendarWidget calendar = new CalendarWidget();
	private Label status = new Label();
	private static Fitness singleton;
	private String serviceurl = "http://localhost/fitnessDev/index.php";
	private HashMap vars = new HashMap();
	
	public static Fitness get() {
	    return singleton;
	  }
	
	public String getAdminFeedKey() {
		return getExternalProperty("adminfeedkey");
	}
	
	public int getDefaultRowLimit() {
		return Integer.parseInt(getExternalProperty("rowlimit"));
	}
	
	public String getFeedKey() {
		return getExternalProperty("feedkey");
	}
	
	public boolean isRSSEnabled() {
		if(getExternalProperty("rss").equals("1")) {
			return true;
		} else {
			return false;
		}
	}
	
	public String getExternalProperty(String key) {
		if(vars.containsKey(key)) {
			return (String) vars.get(key);
		} else {
			return "";
		}
	}
	
	public void setExternalProperty(String key,String value) {
		vars.put(key,value);
	}
	
	public String getURL() {
		return getExternalProperty("serviceurl");
	}

	public void updateStatus(String s) {
		status.setText(s);
		Logger.info(s);
	}
	
	public void updateResultingStatus(String s) {
		updateStatus(s);
	}
	
	public void comunicateWithServer(Vector entries,RequestCallback rc) {
		updateStatus("Connecting...");
		HTTPUtil.doPost(getURL(),entries.toArray(),rc); 
	}
	
	private void init() {
		
		singleton = this;
		
		vars.put("rowlimit","25");
		vars.put("loggerlevel","WARN");
		vars.put("serviceurl",serviceurl);
		vars.put("initwindow","summary");
		
		Dictionary dictionary = Dictionary.getDictionary("variables");
		Object[] a = dictionary.keySet().toArray();
		for(int i=0;i<a.length;i++) {
			String key = (String) a[i];
			if(dictionary.get(key).length() > 0) {
				vars.put(key,dictionary.get(key));
			}
		}
		
		Logger.setLoggingLevel((String)vars.get("loggerlevel"));
		
	}
	
	public void load() {
		RootPanel.get("windows-panel").clear();
		RootPanel.get("menu").clear();
		
		if(vars.containsKey("settings") && vars.get("settings").equals("0")) {
			RootPanel.get("windows-panel").add(windowsManager);
			new CustomFormWindow(null, new EditUserForm(new Integer(-1))).show();
		} else {
			RootPanel.get("windows-panel").add(windowsManager);
			RootPanel.get("menu").add(new MainMenu());
			String initwindow = getExternalProperty("initwindow");
			Logger.debug("initwindow: "+initwindow);
			if(initwindow.equals("summary")) {
				new CustomFormWindow(null,new ViewSummaryForm()).show();
			} else if(initwindow.equals("settings")) {
				new CustomFormWindow(null, new EditUserForm(new Integer(-1))).show();
			} else if(initwindow.equals("exercise")) {
				new CustomRBWindow(new ExerciseRB()).show();
			} else if(initwindow.equals("addexercise")) {
				new CustomFormWindow(null, new AddExerciseForm()).show();
			} else if(initwindow.equals("measurements")) {
				new CustomRBWindow(new MeasurementsRB()).show();
			} else if(initwindow.equals("addmeasurement")) {
				new CustomFormWindow(null,new AddMeasurementsForm()).show();
			} 
		}
	}
	
	public void onModuleLoad() {
		init();
		load();
	}

	public static native void open(String url) /*-{
	  $wnd.open(url);
	}-*/;
	
	public static native void open(String url,String title) /*-{
	  $wnd.open(url,title);
	}-*/;
	
	public static native void open(String url,String title,String specs) /*-{
	  $wnd.open(url,title,specs);
	}-*/;
	
	private native void reloadPage() /*-{
	 $wnd.location.reload();
	 }-*/;
}
